package com.uusee.crawler.pageprocessor.video.sina;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.DomainUtils;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.shipshape.sp.model.Ugc;

public class SinaUserVideoInterfacePageProcessor extends Processor {
	private Log log = LogFactory.getLog(SinaUserVideoInterfacePageProcessor.class);
	
	public void innerProcess(CrawlURI crawlURI) {
		if (!canProcess(crawlURI)) {
			return;
		}
		Date crawlDate = new Date();
		String crawlUser = "uusee-crawler";
		String videoSite = crawlURI.getSourceSite();
		String crawlUrl = crawlURI.getCrawlUrl();
		String crawlResult = crawlURI.getCrawlResult();
		
		List<CrawlURI> playCrawlURIList = (List<CrawlURI>)crawlURI.getModel();
		if (playCrawlURIList==null) {
			playCrawlURIList = new ArrayList<CrawlURI>();
			crawlURI.setModel(playCrawlURIList);
		}
		 
		try {
			JSONObject jsonObject = JSONObject.fromObject( crawlResult ); 
			String code = "";
			if (jsonObject.containsKey("code")) {
				code = jsonObject.getString("code");
			}
			JSONArray items = new JSONArray();
			if (jsonObject.containsKey("data")) {
				JSONObject dataObject = jsonObject.getJSONObject("data");
				items = dataObject.getJSONArray("videoList");
			}
			if (items.size()==0) {
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
				return;
			}
			
			Iterator it = items.iterator();
			while(it.hasNext()){
				JSONObject item = (JSONObject)it.next();
				
				String vid = item.getString("videoid");
				String title = PageProcessorUtils.unicodeToChinese(item.getString("nameAll"));
				String logo = item.getString("videoImage");
				
				int length = item.getInt("timeLength"); 
				String uploadUserid = item.getString("up_userid");
				String uploadUsername = PageProcessorUtils.unicodeToChinese(item.getString("up_username"));
				String uploadDate = item.getString("creatTime");

				if (!logo.startsWith("http://")) {
					logo = "http://"+logo;
				}
				String evid = vid+"-"+uploadUserid;
				String playUrl = "http://you.video.sina.com.cn/b/"+evid+".html";
				String playSite = PageProcessorUtils.getValue("http://([^/]*?)/", playUrl);
				String domain = DomainUtils.getDomain(playUrl);
				String shareUrl = "http://p.you.video.sina.com.cn/player/outer_player.swf?auto=1&vid="+vid+"&uid="+uploadUserid;
				String uploadUserblog = "http://you.video.sina.com.cn/m/"+uploadUserid;
				
				Ugc ugc = new Ugc();
				ugc.setVideoSite(videoSite);
				ugc.setDomain(domain);
				ugc.setSourceSite(playSite);
				ugc.setPlayUrl(playUrl);
				ugc.setTitle(title);
				ugc.setLogo(logo);
				ugc.setLength(length);
				//ugc.setQuality(quality);
				//ugc.setChannel(channel);
				//ugc.setCategory("");
				//ugc.setTags(tags);
				//ugc.setSummary(summary);
				ugc.setVid(vid);
				ugc.setEvid(evid);
				ugc.setOriLogo(logo);
				ugc.setShareUrl(shareUrl);
				//ugc.setTotalPageView(pv);
				//ugc.setTotalComment(totalCommentCount);
				//ugc.setTotalFav(totalFavCount);
				//ugc.setFileExt(fileExt);
				//ugc.setFileSize(fileSize);
				ugc.setUploadUserid(uploadUserid);
				ugc.setUploadUsername(uploadUsername);	
				ugc.setUploadUserblog(uploadUserblog);
				ugc.setUploadDate(uploadDate);
				ugc.setLogoStatus("0");
				ugc.setStatus("1");
				ugc.setCrawlUser(crawlUser);
				ugc.setCrawlDate(crawlDate);
				ugc.setUpdateUser(crawlUser);
				ugc.setUpdateDate(crawlDate);

				
				CrawlURI playCrawlURI = new CrawlURI();
				playCrawlURI.setSourceSite(videoSite);
				playCrawlURI.setCrawlUrl(playUrl);
				playCrawlURI.setCharset("utf-8");
				playCrawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
				playCrawlURI.setModel(ugc);
				
				if (StringUtils.isEmpty(vid) || StringUtils.isEmpty(uploadUserid)) {
					continue;
				}
				if (StringUtils.isEmpty(title) || length==0) {
					log.info(playUrl+"-数据不可用。(title="+title+";length="+length+")");
					ugc.setStatus("0");
					playCrawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
				}
				playCrawlURIList.add(playCrawlURI);
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
			}
		} catch (Exception e) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
			throw new RuntimeException("处理用户视频页失败。");
		}
	}
	
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}
}
